//
// Created by YangShuai on 2022/12/11.
//

/**
  ******************************************************************************
  * @file    driver_remote.h
  * @author  XJTU ROBOMASTER Team
  * @brief   This file contains all the functions prototypes for the remote
  *          driver.
  ******************************************************************************
  * @attention
  *
  * Please add comments after adding or deleting functions to ensure code
  * specification.
  *
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef DRIVER_REMOTE_H
#define DRIVER_REMOTE_H

#define USE_REMOTE_DATA     0
#define RC_FRAME_LENGTH     (20u)

#define MOUSEERRORTOLERANCE (0.00003f)
#define RC_CH_VALUE_OFFSET ((uint16_t)1024)
#define RC_CH_VALUE_MAX ((uint16_t)1684)
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "cmsis_os.h"
#include "driver_chassis.h"

extern osSemaphoreId_t RemoteSemHandle;
/* Exported types ------------------------------------------------------------*/
typedef enum
{
    SwitchUp=1,
    SwitchDown=2,
    SwitchMid=3,
}SwitchEnum;

typedef enum
{
    False=0,
    True=1,
}MousePressEnum;

#pragma pack (1)
typedef union
{
    struct{
        struct{
            uint16_t Ch0:11;
            uint16_t Ch1:11;
            //此处占用22个字节，接下来补齐
            uint16_t Ch2_l:2;
            uint16_t	Ch2_h:9;
            uint16_t Ch3:11;
            uint8_t	s1:2;
            uint8_t	s2:2;
        }RCValue;

        struct{
            int16_t x;
            int16_t y;
            int16_t z;
            uint8_t Press_l;
            uint8_t Press_r;
        }Mouse;

        struct
        {
            uint8_t KeyW:1;
            uint8_t KeyS:1;
            uint8_t KeyA:1;
            uint8_t KeyD:1;
            uint8_t KeyShift:1;
            uint8_t KeyCtrl:1;
            uint8_t KeyQ:1;
            uint8_t KeyE:1;
            uint8_t KeyR:1;
            uint8_t KeyF:1;
            uint8_t KeyG:1;
            uint8_t KeyZ:1;
            uint8_t KeyX:1;
            uint8_t KeyC:1;
            uint8_t KeyV:1;
            uint8_t KeyB:1;
        }Key;
    } RemoteDataProcessed;

    uint8_t RemoteDataRaw[RC_FRAME_LENGTH];
}RemoteDataRaw_Union;
#pragma pack()

typedef struct
{
    //摇杆
    float Channel_0;
    float Channel_1;
    float Channel_2;
    float Channel_3;

    SwitchEnum RightSwitch;
    SwitchEnum LeftSwitch;

    float MouseX;
    float MouseY;
    float MouseZ;

    MousePressEnum LeftMousePress;
    MousePressEnum RightMousePress;

    uint8_t KeyW:1;
    uint8_t KeyS:1;
    uint8_t KeyA:1;
    uint8_t KeyD:1;
    uint8_t KeyShift:1;
    uint8_t KeyCtrl:1;
    uint8_t KeyQ:1;
    uint8_t KeyE:1;
    uint8_t KeyR:1;
    uint8_t KeyF:1;
    uint8_t KeyG:1;
    uint8_t KeyZ:1;
    uint8_t KeyX:1;
    uint8_t KeyC:1;
    uint8_t KeyV:1;
    uint8_t KeyB:1;

    uint8_t FlagValidity;
} RemoteDataProcessed_t;

typedef struct
{
    float ChassisSpeedX;
    float ChassisSpeedY;
    float PitchIncrement;
    float YawIncrement;

    uint8_t	Friction;
    uint8_t	FeedMotor;
    uint8_t	Magazine;
    uint8_t	Laser;
    uint8_t	ShakeEnable;
    uint8_t	FlagChangeFollow;

    SwitchEnum s1;
    SwitchEnum s2;
}RemoteData_t;

typedef struct {
    UART_HandleTypeDef *huart;

    RemoteDataRaw_Union remote_data_raw;
    RemoteDataProcessed_t remote_data_processed;
    RemoteData_t remote_data;
} Remote_message_t;


void Remote_DMA_enable();
RemoteDataProcessed_t RemoteDataProcess(RemoteDataRaw_Union RemoteDataRaw);
RemoteData_t RemoteDataCalculate(RemoteDataProcessed_t	RemoteDataReceive);
uint8_t RemoteTaskControl();

void update_chassisControlData(ChassisControlDataStruct  *chassisControlData);

#endif //DRIVER_REMOTE_H

/************************ (C) COPYRIGHT XJTU ROBOMASTER ********END OF FILE****/
